﻿using System;
using System.Xml;
using System.Data;
using System.Collections.ObjectModel;
using SharePointSearchBench.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Diagnostics;
using Microsoft.Office.Server.Search.Query;

namespace SharePointSearchBench.Tests
{
    [TestClass()]
    public class ConditionTest
    {
        [TestMethod()]
        public void TestDefaultPropertiesFreeText()
        {
            string phrase = "dog cat llama";
            Condition condition = Condition.FreeText(phrase);
            string output = condition.ToString();

            string expected = string.Format("(FREETEXT(DEFAULTPROPERTIES, '{0}'))", phrase);
            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestContains()
        {
            string field = "blah";
            string fieldValue = "foo";
            Condition condition = Condition.Contains(field, fieldValue);
            string output = condition.ToString();

            string expected = string.Format("(CONTAINS({0}, '{1}'))", field, fieldValue);
            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestFromScope()
        {
            string name = "blah";
            Condition condition = Condition.FromScope(name);
            string output = condition.ToString();

            string expected = string.Format("(\"Scope\" = '{0}')", name);
            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestStringComparison()
        {
            string field = "blah";
            string fieldValue = "foo";
            Condition condition = Condition.StringComparison(field, fieldValue);
            string output = condition.ToString();

            string expected = string.Format("({0} = '{1}')", field, fieldValue);
            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestNumericComparison()
        {
            string field = "blah";
            int fieldValue = int.MaxValue;
            Condition condition = Condition.NumericComparison(field, fieldValue);
            string output = condition.ToString();

            string expected = string.Format("({0} = {1})", field, fieldValue);
            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestAnd()
        {
            string field1 = "blah";
            string field2 = "blah2";
            string value1 = "asdfd";
            string value2 = "qwerty";

            Condition condition1 = Condition.StringComparison(field1, value1);
            Condition condition2 = Condition.StringComparison(field2, value2);

            condition1.And(condition2);

            string output = condition1.ToString();
            string expected = string.Format("(({0} = '{1}') AND ({2} = '{3}'))", field1, value1, field2, value2);

            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestNestedAnd()
        {
            string field1 = "blah";
            string field2 = "blah2";
            string value1 = "asdfd";
            string value2 = "qwerty";
            string field3 = "blah3";
            string value3 = "asdfd3333";

            Condition condition1 = Condition.StringComparison(field1, value1);
            Condition condition2 = Condition.StringComparison(field2, value2);
            Condition condition3 = Condition.StringComparison(field3, value3);

            condition2.And(condition3);
            condition1.And(condition2);

            string output = condition1.ToString();
            string expected = string.Format("(({0} = '{1}') AND (({2} = '{3}') AND ({4} = '{5}')))", 
                field1, value1, field2, value2, field3, value3);

            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestOr()
        {
            string field1 = "blah";
            string field2 = "blah2";
            string value1 = "asdfd";
            string value2 = "qwerty";

            Condition condition1 = Condition.StringComparison(field1, value1);
            Condition condition2 = Condition.StringComparison(field2, value2);

            condition1.Or(condition2);

            string output = condition1.ToString();
            string expected = string.Format("(({0} = '{1}') OR ({2} = '{3}'))", field1, value1, field2, value2);

            Assert.AreEqual(expected, output);
        }

        [TestMethod()]
        public void TestNestedAndAndOr()
        {
            string field1 = "blah";
            string field2 = "blah2";
            string value1 = "asdfd";
            string value2 = "qwerty";
            string field3 = "blah3";
            string value3 = "asdfd3333";

            Condition condition1 = Condition.StringComparison(field1, value1);
            Condition condition2 = Condition.StringComparison(field2, value2);
            Condition condition3 = Condition.StringComparison(field3, value3);

            condition2.And(condition3);
            condition1.Or(condition2);

            string output = condition1.ToString();
            string expected = string.Format("(({0} = '{1}') OR (({2} = '{3}') AND ({4} = '{5}')))",
                field1, value1, field2, value2, field3, value3);

            Assert.AreEqual(expected, output);
        }
    }
}
